<?php
class VehiculosController extends AppController {

	var $name = 'Vehiculos';
	var $uses = array('Vehiculo', 'Proveedor', 'TipoVehiculo', 'MarcaVehiculo');
	var $helpers = array('Html', 'Form' );
	
	function beforeFilter() {		
		$this->checkSession();
		$this->MenuCreator->setModulo($this->name);
		$this->set('menu', $this->getMenuElements());		
	}

	function index() {			
		$this->Vehiculo->recursive = 0;
		$this->set('vehiculos', $this->paginate());
	}

	function buscar() {
		if(!empty($this->data)) {
			if($this->data["Vehiculo"]["tipo"]!=""){
				$this->set('vehiculos', $this->Vehiculo->buscar($this->data["Vehiculo"]["palabra_clave"], $this->data["Vehiculo"]["tipo"]));
			}
			else{
				$this->Session->setFlash('Por favor elige un tipo de búsqueda');
			}
		}		
	}
	
	function ver($id = null) {				
		if(!$id) {
			$this->Session->setFlash('Vehiculo Invalido');
			$this->redirect(array('action'=>'index'), null, true);
		}
		$this->set('Vehiculo', $this->Vehiculo->read(null, $id));
	}

	function agregar() {
		if(!empty($this->data)) {
			$this->cleanUpFields();
			$this->Vehiculo->create();
			
			if($this->Vehiculo->save($this->data)) {
				$this->Session->setFlash('El Vehiculo ha sido agregado');
				$this->redirect(array('action'=>'index'), null, true);
			} else {
				$this->Session->setFlash('El Vehiculo no pudo ser agregado.');
			}
		}
		else{
			$this->set('proveedores', $this->Proveedor->generateList());
			$this->set('tipos', $this->TipoVehiculo->generateList());
			$this->set('marcas', $this->MarcaVehiculo->generateList());
			$this->set(compact('proveedores','vehiculos','marcas'));
		}		
	}

	function editar($id = null) {	
		if(!$id && empty($this->data)) {
			$this->Session->setFlash('Vehiculo Invalido');
			$this->redirect(array('action'=>'index'), null, true);
		}
		if(!empty($this->data)) {
			$this->cleanUpFields();
			if($this->Vehiculo->save($this->data)) {
				$this->Session->setFlash('El Vehiculo ha sido actualizado');
				$this->redirect(array('action'=>'index'), null, true);
			} else {
				$this->Session->setFlash('El Vehiculo no pudo ser actualizado');
			}
		}
		if(empty($this->data)) {
			$this->set('proveedores', $this->Proveedor->generateList());
			$this->set('tipos', $this->TipoVehiculo->generateList());
			$this->set('marcas', $this->MarcaVehiculo->generateList());
			$this->set(compact('proveedores','vehiculos','marcas'));			
			$this->data = $this->Vehiculo->read(null, $id);
		}
	}

	function eliminar($id = null) {
		if(!$id) {
			$this->Session->setFlash('Invalid id for Vehiculo');
			$this->redirect(array('action'=>'index'), null, true);
		}
		if($this->Vehiculo->del($id)) {
			$this->Session->setFlash('El Vehiculo #'.$id.' ha sido eliminado');
			$this->redirect(array('action'=>'index'), null, true);
		}
	}
	
	function registra_kilometraje($id = null){
		if(!empty($this->data)) {
			$this->cleanUpFields();
			if($this->Vehiculo->updateKilometraje($this->data)) {
				$this->Session->setFlash('Se ha registrado el Kilometraje');
				$this->redirect(array('action'=>'index'), null, true);
			} else {
				print_r($this->data);
				$this->Session->setFlash('No se pudo registrar el Kilometraje');				
			}
		}

		$this->set('vehiculos', $this->Vehiculo->generateList());
		$this->set(compact('vehiculos'));
	}
}
?>